package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

public class WitKeyData {
	static FinalDb db;

	/**
	 * 威客详情
	 */
	public static Map<String, Object> getWitkeyDetails(String strId,
			Context context) {
		Map<String, Object> detailList = new HashMap<String, Object>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/workerDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();

		if (users.size() > 0) {
			params.put("token", users.get(0).getToken());
		}
		params.put("id", strId);

		// Log.e("id", "" + strId);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);

			// Log.e("strJson", "" + strJson);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsonData = new JSONObject(
						jsonObject.getString("data"));

				detailList.put("focused", jsonData.getString("focused"));
				detailList.put("nickname", jsonData.getString("nickname"));
				detailList.put("avatar", jsonData.getString("avatar"));
				detailList.put("tagName", jsonData.getString("tagName"));
				detailList.put("percent", jsonData.getString("percent") + "%");
				detailList.put("taskNum", jsonData.getString("taskNum"));
				detailList
						.put("speed_score", jsonData.getString("speed_score"));
				detailList.put("attitude_score",
						jsonData.getString("attitude_score"));
				detailList.put("quality_score",
						jsonData.getString("quality_score"));
				detailList.put("caseInfo", jsonData.getString("caseInfo"));

			} else if (resString.equals("1061")) {
				Toast.makeText(context, "传送参数不能为空", Toast.LENGTH_SHORT).show();
			} else if (resString.equals("1062")) {
				Toast.makeText(context, "传送参数错误", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return detailList;
	}

	/**
	 * 标签
	 * 
	 * @param str
	 * @return
	 */
	public static List<Map<String, Object>> getMyTag(String str) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		try {
			JSONArray jsonarr = new JSONArray(str);

			for (int i = 0; i < jsonarr.length(); i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();

				map.put("tagname", jsonarr.getString(i));

				list.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return list;
	}

	/**
	 * 我的案例
	 * 
	 * @param str
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getMySuccessfulCase(
			String str) {
		ArrayList<HashMap<String, Object>> allCateList = new ArrayList<HashMap<String, Object>>();
		try {
			JSONArray jsonArray = new JSONArray(str);

			// Log.e("jsonArray", "" + jsonArray);

			for (int i = 0; i < jsonArray.length(); i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				JSONObject itemJsonObject = jsonArray.optJSONObject(i);

				map.put("id", itemJsonObject.getString("id"));
				map.put("uid", itemJsonObject.getString("uid"));
				map.put("desc", itemJsonObject.getString("desc"));
				map.put("url", itemJsonObject.getString("url"));
				map.put("title", itemJsonObject.getString("title"));
				map.put("pic", itemJsonObject.getString("pic"));

				map.put("cate_id", itemJsonObject.getString("cate_id"));
				map.put("type", itemJsonObject.getString("type"));
				map.put("pub_uid", itemJsonObject.getString("pub_uid"));
				map.put("view_count", itemJsonObject.getString("view_count"));
				map.put("created_at", itemJsonObject.getString("created_at"));
				allCateList.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return allCateList;
	}
}
